package com.springbootdf.controller;


import com.springbootdf.common.Result;
import com.springbootdf.entity.RuleGame;
import com.springbootdf.entity.Rules;
import com.springbootdf.mapper.RuleGameMapper;
import com.springbootdf.service.IRuleGameService;
import com.springbootdf.service.IRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wy
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/rules")
public class RulesController {

    @Autowired
    private IRulesService rulesService;

    @Autowired
    private IRuleGameService ruleGameService;

    @Autowired
    private RuleGameMapper ruleGameMapper;
    @Transactional
    @PostMapping("/add/{gameId}")
    public Result add(@PathVariable Integer gameId, @RequestBody List<Rules> rules){
        List<Integer> ruleIds=ruleGameMapper.getByGameId(gameId);
        rulesService.removeByIds(ruleIds);

//        ruleGameService.removeById(gameId);
        ruleGameMapper.deleteByGameId(gameId);

        rulesService.saveBatch(rules);
//        ruleGameService.
        for (Rules rule : rules) {
            RuleGame ruleGame = new RuleGame();
            ruleGame.setRuleId(rule.getId());
            ruleGame.setGameId(gameId);
            ruleGameService.save(ruleGame);
        }
        return Result.success();
    }
}

